package com.ctg.itrdc.sysmgr.portal.control;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.pasp.core.PageInfo;
import com.ctg.itrdc.sysmgr.portal.api.dto.StaffDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.IStaffService;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemUserService;
import com.ctg.itrdc.sysmgr.portal.common.entity.CommonResult;
import com.ctg.itrdc.sysmgr.portal.data.entity.Staff;
import com.ctg.itrdc.sysmgr.portal.utils.DTOHelper;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("/staff")
public class StaffController extends SysmgrCURDController<Long, StaffDTO> {

	@Resource
	private IStaffService staffService;

	@Resource
	private ISystemUserService systemUserService;

	@RequestMapping("/page")
	@ResponseBody
	public CommonResult queryPageInfo(StaffDTO dto, @RequestParam(defaultValue = "0") Integer currentPage,
			@RequestParam(defaultValue = "10") Integer perPage) throws Exception {

		PageInfo<StaffDTO> pageInfo = staffService.queryPageInfoByOrg(dto, currentPage, perPage);
		return new CommonResult(HttpStatus.OK.toString(), "分页查询成功", pageInfo);
	}

	@RequestMapping("/insert")
	@ResponseBody
	public CommonResult insert(@Valid StaffDTO dto, BindingResult result) throws Exception {
		DTOHelper.updateDTO(dto);
		DTOHelper.valid(result);
		return new CommonResult(HttpStatus.OK.toString(), "新增数据成功", staffService.insert(dto));
	}

	@RequestMapping("/update")
	@ResponseBody
	public CommonResult updateByPrimaryKey(@Valid StaffDTO dto, BindingResult result) throws Exception {
		DTOHelper.updateDTO(dto);
		DTOHelper.valid(result);
		return new CommonResult(HttpStatus.OK.toString(), "更新数据成功", staffService.updateByPrimaryKey(dto));
	}

	@RequestMapping("/get")
	@ResponseBody
	public CommonResult selectByPrimaryKey(StaffDTO dto) throws Exception {
		StaffDTO result = staffService.selectByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "查询数据成功", result);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public CommonResult deleteByPrimaryKey(StaffDTO dto) throws Exception {
		CommonResult checkStaff = checkStaff(dto);
		if (checkStaff != null) {
			return checkStaff;
		}
		staffService.deleteByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "删除成功");
	}

	/**
	 * 批量删除，不推荐使用
	 * 
	 * @param ids
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/batch/delete")
	@ResponseBody
	public CommonResult batchDeleteByPrimaryKey(String ids, StaffDTO dto) throws Exception {
		List<StaffDTO> dtoList = Lists.newArrayList();
		String[] idStr = ids.split(",");
		for (String i : idStr) {
			long id = Long.parseLong(i);
			StaffDTO tmp = new StaffDTO();
			tmp.setStaffId(id);
			
			CommonResult checkStaff = checkStaff(tmp);
			if (checkStaff != null) {
				return checkStaff;
			}
			
			dtoList.add(tmp);
		}

		staffService.batchDeleteByPrimaryKey(dtoList);
		return new CommonResult(HttpStatus.OK.toString(), "删除成功");
	}

	private CommonResult checkStaff(StaffDTO dto) {
		SystemUserDTO systemUserDTO = new SystemUserDTO();
		systemUserDTO.setStaffId(dto.getStaffId());
		List<SystemUserDTO> users = systemUserService.selectByExample(systemUserDTO);
		if (users != null && users.size() != 0) {
			return new CommonResult(HttpStatus.INTERNAL_SERVER_ERROR.toString(), "员工删除失败，请先删除所属员工的系统用户");
		}
		return null;
	}
}
